﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Entry;
using Cofe.Core.EntryType;
using Cofe.Core.Implements;
using Cofe.Data;

namespace Cofe.Core.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class EFCofeSystemInfoConfig : IDatabaseConfigurator
    {
        public void OnDatabaseCreating(CofeDbContext context)
        {
            context.Database.ExecuteSqlCommand(
                    "ALTER TABLE dbo.CofeSystemInfo ADD CONSTRAINT UK_IdType UNIQUE (Id, Type)");
        }

        public void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var cofeSystemInfoConfig = modelBuilder.Entity<CofeSystemInfo>();         

            cofeSystemInfoConfig.ToTable("CofeSystemInfo");
            cofeSystemInfoConfig.HasKey(e => new { e.Id, e.Type });

            cofeSystemInfoConfig.Property(e => e.Type).HasMaxLength(50).IsRequired();

            cofeSystemInfoConfig.Ignore(e => e.Position);
            cofeSystemInfoConfig.Ignore(e => e.CreationTimeUtc);
            cofeSystemInfoConfig.Ignore(e => e.LastWriteTimeUtc);
            cofeSystemInfoConfig.Ignore(e => e.DynamicProperties);
            cofeSystemInfoConfig.Ignore(e => e.CachedProperties);
            cofeSystemInfoConfig.Ignore(e => e.EntryTypeInfo);
            cofeSystemInfoConfig.Ignore(e => e.Entity);
            cofeSystemInfoConfig.Ignore(e => e.Name);
            cofeSystemInfoConfig.Ignore(e => e.Label);            
            cofeSystemInfoConfig.Ignore(e => e.ParseName);
            cofeSystemInfoConfig.Ignore(e => e.InterfaceProviders);
            cofeSystemInfoConfig.Ignore(e => e.Properties);
            cofeSystemInfoConfig.Ignore(e => e.Links);

            
            cofeSystemInfoConfig.Map<EntryTypeInfo>(m =>
            {
                //m.MapInheritedProperties();
                m.Requires("Discriminator").HasValue(Cofe.Core.EntryTypes.EntryType);
                m.ToTable("EntryTypeInfo");
            });


            //modelBuilder.Entity<CofeDirectoryInfo>().ToTable("CofeDirectoryInfo");
            //modelBuilder.Entity<CofeItemInfo>().ToTable("CofeItemInfo");


            cofeSystemInfoConfig.Map<CofeDirectoryInfo>(m =>
            {
                //m.Requires("Discriminator").HasValue("CofeDirectoryInfo");
                //m.MapInheritedProperties();
                m.ToTable("CofeDirectoryInfo");
            });

            cofeSystemInfoConfig.Map<CofeItemInfo>(m =>
            {
               // m.Requires("Discriminator").HasValue("CofeItemInfo");
                //m.MapInheritedProperties();
                m.ToTable("CofeItemInfo");
            });


            cofeSystemInfoConfig.Map<CustomEntryList>(m =>
            {
                //m.MapInheritedProperties();
                m.Requires("Discriminator").HasValue(Cofe.Core.EntryTypes.CustomEntryList);
                m.ToTable("CustomEntryList");
            });


            cofeSystemInfoConfig.Map<AutoEntryList>(m =>
            {
                //m.MapInheritedProperties();
                m.Requires("Discriminator").HasValue(Cofe.Core.EntryTypes.AutoEntryList);
                m.ToTable("AutoEntryList");
            });

            cofeSystemInfoConfig.Map<EntryLink>(m =>
            {
                //m.MapInheritedProperties();
                m.Requires("Discriminator").HasValue(Cofe.Core.EntryTypes.EntryLink);
                m.ToTable("EntryLink");
            });



        }
    }
}
